//
//  LCFeedbackViewController.m
//  lechuang
//
//  Created by Mr_hi on 2023/3/27.
//  Copyright © 2023 Mooyi. All rights reserved.
//

#import "LCFeedbackViewController.h"
#import "XWXTextView.h"

#import "XWXChoosePhotos.h"
#import "TZTestCell.h"
#import "ImageBrowserViewController.h"

#import "LCMineService.h"


@interface LCFeedbackViewController ()<UICollectionViewDelegateFlowLayout,UICollectionViewDataSource>

@property (weak, nonatomic) IBOutlet XWXTextView *contentView;

@property (weak, nonatomic) IBOutlet UILabel *countLb;

@property (weak, nonatomic) IBOutlet UICollectionView *imgCollectionView;

/** 选择图片 */
@property (nonatomic,strong) XWXChoosePhotos *chosePht;

@end

@implementation LCFeedbackViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initVariable];
}

- (void)initVariable {
    self.contentView.placeholder = @"请详细描述您的反馈内容";
    [self.imgCollectionView registerClass:[TZTestCell class] forCellWithReuseIdentifier:@"TZTestCell"];
    [LCNotiCenter addObserver:self selector:@selector(handleTextChanged) name:UITextViewTextDidChangeNotification object:nil];
}

-(void)handleTextChanged {
    if (!self.contentView.markedTextRange) {
        if (self.contentView.text.length > 200) {
            self.contentView.text = [self.contentView.text substringToIndex:200];
        }
        self.countLb.text = kFORMAT(@"%ld/200",self.contentView.text.length);
    }
}

#pragma mark - CollectDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.chosePht.allPhotos.count + 1;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat cellW = (ScreenW - 34 - 2 * 10)/3;
    return CGSizeMake(cellW, cellW);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    TZTestCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TZTestCell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor colorWithHexString:@"#ECECEC"];
    cell.videoImageView.hidden = YES;
    if (indexPath.item == self.chosePht.allPhotos.count) {
        [cell.imageView setImage:kGetImage(@"lc_mine_tool_add_photo")];
        cell.deleteBtn.hidden = YES;
    } else {
        if (indexPath.item < self.chosePht.netPhotoArr.count) {
            if ([self.chosePht.netPhotoArr[indexPath.item] isKindOfClass:[NSString class]]) {
                [cell.imageView sd_setImageWithURL:[LCFunction XWX_URLImg:self.chosePht.allPhotos[indexPath.item]] placeholderImage:nil];
            }
        } else if (indexPath.item >= self.chosePht.netPhotoArr.count && indexPath.item < self.chosePht.netPhotoArr.count + self.chosePht.selectedPhotos.count) {
            if ([self.chosePht.selectedPhotos[indexPath.item - self.chosePht.netPhotoArr.count] isKindOfClass:[UIImage class]]) {
                cell.imageView.image = self.chosePht.selectedPhotos[indexPath.item - self.chosePht.netPhotoArr.count];
                cell.asset = self.chosePht.selectedAssets[indexPath.item - self.chosePht.netPhotoArr.count];
            }
        }
        cell.deleteBtn.hidden = NO;
        cell.deleteBtn.tag = indexPath.item+100;
        [cell.deleteBtn addTarget:self action:@selector(deleteBtnClik:) forControlEvents:UIControlEventTouchUpInside];
    }
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.item == self.chosePht.allPhotos.count) {
        if (self.chosePht.allPhotos.count >= 3) {
            [self showErrorText:@"最多只能上传3张图片!"];
        }else{
            [self.chosePht showActionSheetInFatherViewController:self andMaxCount:3 andInitHeight:^(CGFloat height) {
            }];
        }
    } else {
        if (indexPath.item < self.chosePht.netPhotoArr.count) {
            __block NSMutableArray *whole_url_arr = [[NSMutableArray alloc]init];
            [whole_url_arr addObjectsFromArray:self.chosePht.netPhotoArr];
            [ImageBrowserViewController show:self type:PhotoBroswerVCTypeModal index:indexPath.item imagesBlock:^NSArray *{
                return whole_url_arr;
            }];
        } else if (indexPath.item >= self.chosePht.netPhotoArr.count && indexPath.item < self.chosePht.netPhotoArr.count + self.chosePht.selectedPhotos.count) {
            __block NSMutableArray *whole_url_arr = [[NSMutableArray alloc]init];
            [whole_url_arr addObjectsFromArray:self.chosePht.selectedPhotos];
            [ImageBrowserViewController show:self type:PhotoBroswerVCTypeModal index:indexPath.item-self.chosePht.netPhotoArr.count imagesBlock:^NSArray *{
                return whole_url_arr;
            }];
        }
    }
}

- (void)deleteBtnClik:(UIButton *)sender {
    if (sender.tag - 100 < self.chosePht.netPhotoArr.count) {
        [self.chosePht.netPhotoArr removeObjectAtIndex:(sender.tag - 100)];
    } else if (sender.tag - 100 >= self.chosePht.netPhotoArr.count && sender.tag - 100 < self.chosePht.netPhotoArr.count + self.chosePht.selectedPhotos.count) {
        [self.chosePht.selectedPhotos removeObjectAtIndex:sender.tag - 100 - self.chosePht.netPhotoArr.count];
        [self.chosePht.selectedAssets removeObjectAtIndex:sender.tag - 100 - self.chosePht.netPhotoArr.count];
    }
    [self.chosePht.allPhotos removeAllObjects];
    [self.chosePht.allPhotos addObjectsFromArray:self.chosePht.netPhotoArr];
    [self.chosePht.allPhotos addObjectsFromArray:self.chosePht.selectedPhotos];
    [self.imgCollectionView reloadData];
}

-(XWXChoosePhotos *)chosePht {
    if (!_chosePht) {
        _chosePht  = [[XWXChoosePhotos alloc]init];
        kWeakSelf(self);
        [_chosePht setGetAllImagesBlock:^(NSMutableArray *imgArrs) {
            [weakself.imgCollectionView reloadData];
        }];
    }return _chosePht;
}


- (IBAction)backAction:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)saveBtnAction:(UIButton *)sender {
    if (self.contentView.text.length <= 0) {
        [self showErrorText:self.contentView.placeholder];
    } else {
        [LCMineService userGuestWithContext:self.contentView.text Block:^(BOOL isSuccess) {
            if (isSuccess) {
                [self.navigationController popViewControllerAnimated:YES];
            }
        }];
    }
}

@end
